package org.jeecg.modules.equipment.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.equipment.EquipmentCondition;
import org.jeecg.modules.equipment.entity.Equipment;
import org.jeecg.modules.equipment.entity.EquipmentInstallCondition;
import org.jeecg.modules.equipment.model.EquipmentModel;
import org.jeecg.modules.equipment.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: zhuzhuhong
 * @Date: 2019/11/19 14:05
 * @Description:
 */
@RestController
@RequestMapping("/equipment/equipment")
@Log4j2
public class EquipmentController {
    @Autowired
    private EquipmentService equipmentService;

    @GetMapping(value = "/list")
    public Result<IPage<EquipmentModel>> getEquipmentList(
            @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "ids",required = false) String ids,
            @RequestParam(value = "enabled",required = false) Integer enabled,
            @RequestParam(value = "leasestate",required = false) Integer leasestate,
            @RequestParam(value = "commodityId",required = false) String commodityId
    ) {
        Result<IPage<EquipmentModel>> result = new Result<>();
        try {
            //获取当前登录用户
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            //使用建造者模式创建条件实体类
            EquipmentCondition condition = EquipmentCondition.builder()
                    .commodityId(commodityId)
                    .enabled(enabled)
                    .ids(ids)
                    .leasestate(leasestate)
                    .userId(user.getId())
                    .build();
            List<EquipmentModel> equipmentList = equipmentService.findEquipmentList(condition);
            IPage<EquipmentModel> page = new Page<>(pageNo, pageSize);
            page.setRecords(equipmentList);
            result.setResult(page);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("后台异常");
        }
        return result;
    }

    @PostMapping(value = "/add")
    public Result<Object> addEquipment(@RequestBody Equipment equipment) {
        Result<Object> result = new Result<>();
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            equipment.setCreateBy(user.getRealname());
            boolean saveResult = equipmentService.save(equipment);
            if (!saveResult) {
                result.error500("操作失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("后台异常");
        }

        return result;
    }

    @PostMapping(value = "/installFilterCartridge")
    public Result<Object> installFilterCartridge(@RequestBody EquipmentInstallCondition condition) {
        Result<Object> result = new Result<>();
        try {
            //判断参数是否符合要求
            Double lossratioDouble = StringUtils.isNumeric(condition.getLossratio()) == true ? Double.valueOf(condition.getLossratio()) : 0;

            if (lossratioDouble>=0) {
                result.error500("参数异常");
                return result;
            }
            boolean installResult = equipmentService.installFilterCartridge(condition);
            if (!installResult) {
                result.error500("操作失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("后台异常");
        }
        return result;
    }



}
